﻿using System;
using fastCSharp;

namespace diantou.web.tutorial
{
    /// <summary>
    /// 教程视图
    /// </summary>
    /// <typeparam name="tutorialType"></typeparam>
    abstract class view<viewType> : web.view<viewType>
        where viewType : view<viewType> 
    {
        /// <summary>
        /// 教程
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.tutorial tutorial;
        /// <summary>
        /// 用户教程
        /// </summary>
        protected userTutorial userTutorial
        {
            get { return new userTutorial { Tutorial = tutorial, User = currentUser }; }
        }
        /// <summary>
        /// 基础教程集合
        /// </summary>
        protected userTutorial[] baseTutorials
        {
            get
            {
                return tutorial.Remote.BaseTutorials.getArray(value => new userTutorial { Tutorial = value, User = currentUser });
            }
        }
        /// <summary>
        /// 提高教程集合
        /// </summary>
        protected userTutorial[] upTutorials
        {
            get
            {
                return tutorial.Remote.UpTutorials.getArray(value => new userTutorial { Tutorial = value, User = currentUser });
            }
        }
        /// <summary>
        /// 课程集合
        /// </summary>
        protected userCourse[] courses
        {
            get
            {
                return tutorial.Remote.Courses.getArray(value => new userCourse { Course = value, User = currentUser });
            }
        }
        /// <summary>
        /// 学习用户集合
        /// </summary>
        protected focusImage registerImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusTutorial.getUserIds(tutorial.Id, diantou.dataModel.focus.type.Register, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 毕业用户集合
        /// </summary>
        protected focusImage passImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusTutorial.getUserIds(tutorial.Id, diantou.dataModel.focus.type.Accepted, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tutorialId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int tutorialId)
        {
            return load(tutorialId);
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="tutorialId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(int tutorialId, string searchEnginePath = null)
        {
            if (tutorialId == 0)
            {
                checkSearchEngine(searchEnginePath ?? diantou.dataModel.path.pub.Default.TutorialListPath, webConfig.NotFound404);
                return false;
            }
            tutorial = diantou.dataAccess.clientCache.Tutorial[tutorialId];
            if (tutorial != null && (tutorial.IsShow || currentUser.IsTutorialEditor(tutorial))) return true;
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
